/**
 *MenuBar.java
 *@anthor Daniel Liu
 *2006-4-25 21:22:04
 **/
package org.lex.utils.logging.handler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;

class MainMenuBar extends JMenuBar implements ActionListener {
	private final String SaveAC = "Save";

	private final String NewTabAC = "New Tab";
	private final String ScrollAC = "Scroll";
	private final String WrapAC = "Wrap";
	private final String TopAC = "Top";
	private final String LeftAC = "Left";
	private final String BottomAC = "Bottom";
	private final String RightAC = "Right";

	private LoggerMainFrame frame = null;

	private JRadioButtonMenuItem scrollMenuItem = null;
	private JRadioButtonMenuItem wrapMenuItem = null;
	private JRadioButtonMenuItem topMenuItem = null;
	private JRadioButtonMenuItem leftMenuItem = null;
	private JRadioButtonMenuItem bottomMenuItem = null;
	private JRadioButtonMenuItem rightMenuItem = null;

	private JCheckBoxMenuItem hideBorderMenuItem = null;
	private JCheckBoxMenuItem onTopMenuItem = null;

	public MainMenuBar(LoggerMainFrame frame) {
		super();
		this.frame = frame;
		initialize();
	}

	private void initialize() {
		this.add(createFileMenu());
		this.add(createViewMenu());
		this.add(createToolsMenu());
	}

	private JMenu createFileMenu() {
		JMenu fileMenu = createMenu("File");
		fileMenu.add(createMenuItem(this.SaveAC));
		fileMenu.addSeparator();
		JMenuItem exit = new JMenuItem(frame.getExitAction());
		fileMenu.add(exit);
		return fileMenu;
	}

	private JMenu createToolsMenu() {
		JMenu toolsMenu = createMenu("Tools");
		toolsMenu.add(createMenuItem("Null"));
		return toolsMenu;
	}

	private JMenu createViewMenu() {
		JMenu viewMenu = createMenu("View");
		viewMenu.add(createMenuItem(NewTabAC));
		viewMenu.add(createTabLayoutPolicyMenu());
		viewMenu.add(createTabPlacementMenu());
		viewMenu.addSeparator();

		hideBorderMenuItem = new JCheckBoxMenuItem(frame.getHideBorderAction());
		viewMenu.add(hideBorderMenuItem);

		onTopMenuItem = new JCheckBoxMenuItem(frame.getAlwaysOnTopAction());
		viewMenu.add(onTopMenuItem);
		return viewMenu;
	}

	private JMenu createTabLayoutPolicyMenu() {
		JMenu tabLayoutMenu = createMenu("Tab Layout");
		this.scrollMenuItem = this.createRadioButtonMenuItem(ScrollAC, true);
		this.wrapMenuItem = this.createRadioButtonMenuItem(WrapAC, false);
		ButtonGroup group = new ButtonGroup();
		group.add(this.scrollMenuItem);
		group.add(this.wrapMenuItem);

		tabLayoutMenu.add(this.scrollMenuItem);
		tabLayoutMenu.add(this.wrapMenuItem);
		return tabLayoutMenu;
	}

	private JMenu createTabPlacementMenu() {
		JMenu tabPlacementMenu = createMenu("Tab Placement");
		this.topMenuItem = this.createRadioButtonMenuItem(TopAC, true);
		this.leftMenuItem = this.createRadioButtonMenuItem(LeftAC, false);
		this.bottomMenuItem = this.createRadioButtonMenuItem(BottomAC, false);
		this.rightMenuItem = this.createRadioButtonMenuItem(RightAC, false);
		ButtonGroup group = new ButtonGroup();
		group.add(this.topMenuItem);
		group.add(this.leftMenuItem);
		group.add(this.bottomMenuItem);
		group.add(this.rightMenuItem);

		tabPlacementMenu.add(this.topMenuItem);
		tabPlacementMenu.add(this.leftMenuItem);
		tabPlacementMenu.add(this.bottomMenuItem);
		tabPlacementMenu.add(this.rightMenuItem);
		return tabPlacementMenu;
	}

	private JMenu createMenu(String text) {
		JMenu menu = new JMenu();
		menu.setText(text);
		return menu;
	}

	private JMenuItem createMenuItem(String text) {
		JMenuItem item = new JMenuItem();
		item.setText(text);
		item.setActionCommand(text);
		item.addActionListener(this);
		return item;
	}

	private JRadioButtonMenuItem createRadioButtonMenuItem(String text, boolean isSelected) {

		JRadioButtonMenuItem item = new JRadioButtonMenuItem();
		item.setText(text);
		item.setActionCommand(text);
		item.setSelected(isSelected);
		item.addActionListener(this);
		return item;
	}

	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();
		if (ac == SaveAC) {

		} else if (ac == NewTabAC) {
			frame.addTab("test");
		} else if (ac == ScrollAC) {
			frame.getTabPane().setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		} else if (ac == WrapAC) {
			frame.getTabPane().setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
		} else if (ac == TopAC) {
			frame.getTabPane().setTabPlacement(JTabbedPane.TOP);
		} else if (ac == LeftAC) {
			frame.getTabPane().setTabPlacement(JTabbedPane.LEFT);
		} else if (ac == BottomAC) {
			frame.getTabPane().setTabPlacement(JTabbedPane.BOTTOM);
		} else if (ac == RightAC) {
			frame.getTabPane().setTabPlacement(JTabbedPane.RIGHT);
		}
	}
}
